package com.example.baidupushtest;

import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.widget.RemoteViews;

import com.example.baidupushtest.receiver.PushMessageReceiver;

public class MyApplication extends Application {
	public static final String API_KEY = "Gh8mHQQTdZQGqL61pK2pG7A3";
	public static final String ACCESS_TOKEN = "GHkQdPjFGIwzQnmZ1NRuZ9vSOihHIo0x";

	private NotificationManager mNotificationManager;
	private Notification mNotification;
	private static MyApplication mApplication;
    
    private Long channelId = 0L;
    private String userId = "";
	public synchronized static MyApplication getInstance() {
		return mApplication;
	}
	boolean isFirst = true;
	@Override
	public void onCreate() {
		super.onCreate();
		mApplication = this;
		mNotificationManager = (NotificationManager) getSystemService(android.content.Context.NOTIFICATION_SERVICE);
	};
	
	public NotificationManager getNotificationManager() {
		if (mNotificationManager == null)
			mNotificationManager = (NotificationManager) getSystemService(android.content.Context.NOTIFICATION_SERVICE);
		return mNotificationManager;
	}
	/**
	 * 创建挂机图标
	 */
	@SuppressWarnings("deprecation")
	public void showNotification() {

		int icon = R.drawable.notify_general;
		CharSequence tickerText = getResources().getString(
				R.string.hello_world);
		long when = System.currentTimeMillis();
		mNotification = new Notification(icon, tickerText, when);
		
		// 放置在"正在运行"栏目中
		mNotification.flags = Notification.FLAG_ONGOING_EVENT;

		RemoteViews contentView = new RemoteViews(getPackageName(),
				R.layout.notify_status_bar_latest_event_view);
		contentView.setImageViewResource(R.id.icon,
				R.drawable.ic_launcher);
		contentView.setTextViewText(R.id.title, "通知");
		contentView.setTextViewText(R.id.text, tickerText);
		contentView.setLong(R.id.time, "setTime", when);
		// 指定个性化视图
		mNotification.contentView = contentView;

		Intent intent = new Intent(this, MainActivity.class);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				intent, PendingIntent.FLAG_UPDATE_CURRENT);
		// 指定内容意图
		mNotification.contentIntent = contentIntent;
		// 下面是4.0notify
		// Bitmap icon = BitmapFactory.decodeResource(getResources(),
		// heads[mSpUtil.getHeadIcon()]);
		// Notification.Builder notificationBuilder = new Notification.Builder(
		// this).setContentTitle(mSpUtil.getNick())
		// .setContentText(tickerText)
		// .setSmallIcon(R.drawable.notify_general)
		// .setWhen(System.currentTimeMillis())
		// .setContentIntent(contentIntent).setLargeIcon(icon);
		// Notification n = notificationBuilder.getNotification();
		// n.flags |= Notification.FLAG_NO_CLEAR;

		mNotificationManager.notify(PushMessageReceiver.NOTIFY_ID,
				mNotification);
	}

	public boolean isFirst() {
		return isFirst;
	}

	public void setFirst(boolean isFirst) {
		this.isFirst = isFirst;
	}
    public void setChannelId(Long channelId){
        this.channelId = channelId;
    }
    public long getChannelId(){
        return this.channelId;
    }
    public void setUserId(String userId){
        this.userId = userId;
    }
    public String getUserId(){
        return this.userId;
    }
}